package com.huifaapp.mall.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.huifaapp.mall.R;


@SuppressWarnings("unused")
/**
 * 
 * @ClassName: CommonTowBtnDialog
 * @Description: TODO(提示對話框)
 * @author lijs
 * @date 2016年4月27日 下午12:32:47
 *
 */
public class CommonTowBtnDialog extends Dialog implements View.OnClickListener {
	private TextView tv_title;

	private TextView tv_content;
	public Button bt_confirm;
	public Button bt_cancel;
	private Context context;
	private DialogButtonOnClickListener buttonOnClickListener;

	public CommonTowBtnDialog(Context context) {
		super(context, R.style.commob_middle_dialog);
		this.context=context;
		initView();
	}

	private void initView() {
		final View view = LayoutInflater.from(context).inflate(R.layout.dialog_common_tow_btn, null);
		setContentView(view);
		tv_title = (TextView) view.findViewById(R.id.tv_title);
		tv_content = (TextView) view.findViewById(R.id.tv_content);
		bt_confirm = (Button) view.findViewById(R.id.bt_confirm);
		bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
		bt_confirm.setOnClickListener(this);
		bt_cancel.setOnClickListener(this);
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//		lp.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
		dialogWindow.setWindowAnimations(R.style.promptdialog_anim); //设置窗口弹出动画
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);
	}
	/**
	 * 隐藏确认按钮
	 */
	public CommonTowBtnDialog confirmVisibility(){
		bt_confirm.setVisibility(View.GONE);

		return this;
	}
	/**
	 * 設置標題
	 * 
	 * @param text
	 */
	public CommonTowBtnDialog setTitle(String text) {
		tv_title.setText(text);
		return this;
	}

	/**
	 * 設置內容
	 */
	public CommonTowBtnDialog setContent(String text) {
		tv_content.setText(text);
		return this;
	}

	/**
	 * 左边按钮的文字
	 */
	public CommonTowBtnDialog setLeft(String text) {
		bt_confirm.setText(text);
		return this;
	}

	public CommonTowBtnDialog setRight(String text) {
		bt_cancel.setText(text);
		return this;
	}

	/**
	 * 設置內容左邊的圖標
	 */
	@SuppressWarnings("deprecation")
	public CommonTowBtnDialog setLeftContentIco(int id) {
		Drawable ico;
		Resources res = getContext().getResources();
		ico = res.getDrawable(id);
		ico.setBounds(0, 0, ico.getMinimumWidth(), ico.getMinimumHeight());
		tv_content.setCompoundDrawables(ico, null, null, null);
		return this;
	}

	public interface DialogButtonOnClickListener {
		void onLeft(View v);
		void onRight(View v);
	}

	/**
	 * 左右按钮的点击事件
	 * 
	 * @param buttonOnClickListener
	 */
	public CommonTowBtnDialog setDialogButtonOnClickListener(DialogButtonOnClickListener buttonOnClickListener) {
		this.buttonOnClickListener = buttonOnClickListener;
		return this;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_confirm:
			if (v != null) {
				dismiss();
				buttonOnClickListener.onLeft(v);
			}
			break;
		case R.id.bt_cancel:
			if (v != null) {
				dismiss();
				buttonOnClickListener.onRight(v);
			}
			break;

		default:
			break;
		}
	}
}
